package mysql

import (
	"hema-fresh-api/hema/hema"

	"gorm.io/gorm"
)

type Hema struct {
	gorm.Model
	Name     string  `gorm:"type:varchar(30);not null;comment:'名称'"`
	Image    string  `gorm:"type:varchar(500);not null;comment:'图像'"`
	Price    float64 `gorm:"type:decimal(10,2);not null;comment:'价格'"`
	StatusId int64   `gorm:"type:int(11);not null;comment:'分类id'"`
}

func (h *Hema) HemaAdd(db *gorm.DB) error {
	return db.Debug().Create(h).Error
}

func (h *Hema) FindHemaByName(db *gorm.DB, name string) error {
	return db.Debug().Where("name=?", name).First(h).Error
}

func (h *Hema) HemaList(db *gorm.DB, in *hema.HemaListRequest, lists []*hema.HemaList) ([]*hema.HemaList, error) {
	err2 := db.Model(&Hema{}).
		Select("hema.id as hema_id",
			"name",
			"image",
			"price").
		Scopes(Paginate(int(in.Page), int(in.Size))).Find(&lists).Error
	return lists, err2
}
